//
//  BookViewController.m
//  iBook
//
//  Created by wangjing on 14/12/3.
//  Copyright (c) 2014年 wikiflyer. All rights reserved.
//

#import "BookViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "BookTableViewCell.h"
#import "MenuButton.h"
#import "BookCateViewController.h"

@interface BookViewController ()
@property(nonatomic,strong)NSArray *books;      //所有书的信息
@end

@implementation BookViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initNavigation];
    
    //UITableView相关  设置数据源和监听器  --------------------------------
    _bookTableView.dataSource = self;
    _bookTableView.delegate = self;
    //Cell相关        -------------------------------------------------
    //设置Cell的高度
    self.bookTableView.rowHeight = 70;
    
}
/**
 *  初始化导航栏
 */
-(void)initNavigation{
    //导航栏相关     -------------------------------------------------
    //    self.navigationItem.leftBarButtonItem = [UIBarButtonItem item]
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Select" style:UIBarButtonItemStyleDone target:self action:@selector(selectClick)];
    UIImage *leftImg = [UIImage imageNamed:@"tab1.png"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:leftImg style:UIBarButtonItemStyleDone target:self action:nil];
    
    CGRect frame = CGRectMake(0.0, 0.0, 200.0, self.navigationController.navigationBar.bounds.size.height);
    MenuButton *menuButton = [[MenuButton alloc]initWithFrame:frame];
    menuButton.title.text = @"All Book";
    [menuButton addTarget:self action:@selector(MenuButtonClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = menuButton;
}
/**
 *  顶部中间点击
 */
-(void)MenuButtonClick{
    NSLog(@"MenuButtonClick");
    BookCateViewController *cateVC = [[BookCateViewController alloc]init];
    cateVC.hide = true;
    [self.navigationController pushViewController:cateVC animated:YES];
//    [self presentModalViewController:cateVC animated:YES];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:NO];
}
//加载固定数据（暂时测试使用）
-(NSArray *)books{
    if (_books == nil) {
        NSMutableArray *arrayM = [NSMutableArray array];
        for (int i = 0; i<20; i++) {
//            NSLog(@"index %d",i);
            BookModel *bModel = [[BookModel alloc]init];
            bModel.bookImg = @"book_temp.jpg";
            bModel.bookAuthor = [NSString stringWithFormat:@"Recent %d",i];
            bModel.bookTitle = [NSString stringWithFormat:@"title %d",i];
            [arrayM addObject:bModel];
        }
        _books = [arrayM mutableCopy];
    }
//    NSLog(@"count %d",_books.count);
    return _books;
}

/**
 *  选择按钮点击事件
 */
-(void)selectClick{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"选择提示框" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
    [alert show];
}

#pragma mark TableView DataSource
///多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.books.count;
}
//每组每行的数据  设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //1、创建Cell
    BookTableViewCell *cell = [BookTableViewCell bookCellWithTableView:tableView];
//    BookModel *bModel = [BookModel initData:@"book_temp.jpg" Title:@"标题" Author:@"作者"];

    BookModel *bModel = self.books[indexPath.row];
    cell.bookModel = bModel;
    return cell;
}
//点击Item触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BookModel *selModel = self.books[indexPath.row];
    selModel.bookAuthor = @"refresh";
    //刷新指定行 即刷新点击的Cell
    NSIndexPath *path = [NSIndexPath indexPathForItem:indexPath.row inSection:0];
    [self.bookTableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];
}

/**
 * SegmentedControl 的点击选择事件
 *  @param sender ;
 */
- (IBAction)segSelect:(UISegmentedControl *)sender {
//    NSLog(@"select is %d",sender.selectedSegmentIndex);
    switch (sender.selectedSegmentIndex) {
        case 0:
            [self showRecentData];
            break;
        case 1:
            [self showSecondData];
            break;
        case 2:
            [self showThirdData];
            break;
        case 3:
            [self showFourData];
            break;
        default:
            break;
    }
    //调用该方法会刷新所有数据
    [self.bookTableView reloadData];
}
//点击第一个标签时加载的数据
-(void)showRecentData{
    NSMutableArray *arrayM = [NSMutableArray array];
    for (int i = 0; i<20; i++) {
        BookModel *bModel = [[BookModel alloc]init];
        bModel.bookImg = @"book_temp.jpg";
        bModel.bookAuthor = [NSString stringWithFormat:@"Recents %d",i];
        bModel.bookTitle = [NSString stringWithFormat:@"Recents %d",i];
        [arrayM addObject:bModel];
    }
    _books = [arrayM mutableCopy];
}
//点击第二个标签时加载的数据
-(void)showSecondData{
        NSMutableArray *arrayM = [NSMutableArray array];
        for (int i = 0; i<20; i++) {
            BookModel *bModel = [[BookModel alloc]init];
            bModel.bookImg = @"book_temp.jpg";
            bModel.bookAuthor = [NSString stringWithFormat:@"Titles %d",i];
            bModel.bookTitle = [NSString stringWithFormat:@"Titles %d",i];
            [arrayM addObject:bModel];
        }
        _books = [arrayM mutableCopy];
}
//点击第三个标签时加载的数据
-(void)showThirdData{
        NSMutableArray *arrayM = [NSMutableArray array];
        for (int i = 0; i<20; i++) {
            BookModel *bModel = [[BookModel alloc]init];
            bModel.bookImg = @"book_temp.jpg";
            bModel.bookAuthor = [NSString stringWithFormat:@"Authors %d",i];
            bModel.bookTitle = [NSString stringWithFormat:@"Authors %d",i];
            [arrayM addObject:bModel];
        }
        _books = [arrayM mutableCopy];
}
//点击第四个标签时加载的数据
-(void)showFourData{
        NSMutableArray *arrayM = [NSMutableArray array];
        for (int i = 0; i<20; i++) {
            BookModel *bModel = [[BookModel alloc]init];
            bModel.bookImg = @"book_temp.jpg";
            bModel.bookAuthor = [NSString stringWithFormat:@"Categories %d",i];
            bModel.bookTitle = [NSString stringWithFormat:@"Categories %d",i];
            [arrayM addObject:bModel];
        }
        _books = [arrayM mutableCopy];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
*/
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
